﻿using Microsoft.Extensions.Configuration;

using RestSharp;
using System.Net;
using Web.MVC.Helper;

namespace Web.Mvc.Helper
{
    public class GatewayServiceHelper : IServiceHelper
    {
        private IConfiguration _configuration;
        public GatewayServiceHelper(IConfiguration configuration) 
        {
        
            _configuration = configuration;
        }
        public async Task<string> GetOrder(string accessToken)
        {
            //var client = new RestClient("https://localhost:7296/Identity");
            var client = new RestClient(_configuration.GetValue<string>("APIGatewayAddress"));//认证中心地址//apigateway// "https://apigateway:7236"
            var request = new RestRequest("/order", Method.Get);
      
            request.AddHeader("Authorization", "Bearer " + accessToken);

            var response = await client.ExecuteAsync(request);
            if (response.StatusCode != HttpStatusCode.OK)
            {
                return response.StatusCode + " " + response.Content;
            }
            return response.Content;
        }

        public async Task<string> GetProduct(string accessToken)
        {
            // var client = new RestClient("https://localhost:7128");
             var client = new RestClient(_configuration.GetValue<string>("APIGatewayAddress"));//http://apigateway:9070
            var request = new RestRequest("/product", Method.Get);
            request.AddHeader("Authorization", "Bearer " + accessToken);

            var response = await client.ExecuteAsync(request);
            if (response.StatusCode != HttpStatusCode.OK)
            {
                return response.StatusCode + " " + response.Content;
            }
            return response.Content;
        }

        public void GetServices()
        {
            Console.WriteLine("执行了");
            //throw new NotImplementedException();
        }
    }
}
